package com.passive.lock.service.imp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.passive.lock.mapper.TMenuMapper;
import com.passive.lock.mapper.TMenuMapperCustom;
import com.passive.lock.model.po.TMenu;
import com.passive.lock.model.po.TMenuExample;
import com.passive.lock.model.vo.TMenuVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.MenuService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.Result;
import com.passive.lock.utils.tree.TreeMenuUtil;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	@Resource(name = "TMenuMapperCustom")
	private TMenuMapperCustom menuMapperCustom;
	
	@Resource(name = "TMenuMapper")
	private TMenuMapper menuMapper;
	
	@Override
	public Result menuTree(TUserVo tUserVo) {
		List<TMenu> list = menuMapperCustom.menuTree(tUserVo.getRoleid());
		List<TMenuVo> listTree = new ArrayList<TMenuVo>();
		for (TMenu tree : list) {
			TMenuVo TMenuVo = new TMenuVo();
			DozerMapperUtils.map(tree, TMenuVo);
			listTree.add(TMenuVo);
		}
		List<TMenuVo> newTreeDataList = TreeMenuUtil.getfatherNode(listTree);
		return Result.ok(newTreeDataList);
	}

	@Override
	public Result menuRight(TUserVo tUserVo) {
		List<TMenu> list = menuMapperCustom.menuRight(tUserVo.getRoleid());
		return Result.ok(list);
	}


	@Override
	public Result menuList(String pcode) {
		TMenuExample example = new TMenuExample();
		TMenuExample.Criteria Criteria = example.createCriteria();
		Criteria.andPcodeEqualTo(pcode);
		List<TMenu> list = menuMapper.selectByExample(example);
		return Result.ok(list);
	}

	@Override
	public Result addMenu(TMenuVo tMenuVo) {
		TMenu tMenu = new TMenu();
		DozerMapperUtils.map(tMenuVo, tMenu);
		menuMapper.insert(tMenu);
		return Result.ok(tMenu);
	}

	@Override
	public Result updateMenu(TMenuVo tMenuVo) {
		TMenu tMenu = new TMenu();
		DozerMapperUtils.map(tMenuVo, tMenu);
		menuMapper.updateByPrimaryKey(tMenu);
		return Result.ok();
	}

	@Override
	public Result deleteMenu(TMenuVo tMenuVo) {
		int count =menuMapper.deleteByPrimaryKey(tMenuVo.getId());
		return Result.ok(count);
	}
}
